
//
//  ZDQTopicViewController.m
//  Alltelevisionnews
//
//  Created by dllo on 15/11/29.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ZDQTopicViewController.h"

@interface ZDQTopicViewController ()<UITableViewDataSource, UITableViewDelegate, ZDQTopicNewsTableViewCellDelegate>

@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) UIView *hearderView;
@property(nonatomic, strong) UIImageView *photoImageView;
@property(nonatomic, strong) UILabel *describeLabel;
@property(nonatomic, strong) NSArray *Arr;
@property(nonatomic, strong) NSMutableArray *array;
@property(nonatomic, strong) SLBannerView *banner;
@property(strong, nonatomic) NSArray *bannerImageURLArray;
@property(strong, nonatomic) NSArray *bannerTitleStrArray;
@property(nonatomic, strong) NSMutableArray *phototArray;
@property(nonatomic, strong) NSMutableArray *titleArray;
@property(nonatomic, strong) NSArray *imageArr;
@property(nonatomic, strong) MBProgressHUD *hud;

@end

@implementation ZDQTopicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    // Do any additional setup after loading the view.
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT - 60 * KHEIGHT / 667) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    self.hearderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH, 350 * KHEIGHT / 667)];
    self.tableView.tableHeaderView = self.hearderView;
//    self.hearderView.backgroundColor = [UIColor redColor];

    self.photoImageView = [[UIImageView alloc] init];
    [self.hearderView addSubview:self.photoImageView];
    [self.photoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.hearderView).with.offset(0);
        make.left.equalTo(self.hearderView).with.offset(0);
        make.right.equalTo(self.hearderView).with.offset(0);
        make.height.mas_equalTo(50);
        
        
    }];
    
    self.describeLabel = [[UILabel alloc] init];
    [self.hearderView addSubview:self.describeLabel];
    self.describeLabel.numberOfLines = 0;
    self.describeLabel.font = [UIFont systemFontOfSize:15];
    [self.describeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.photoImageView.mas_bottom).with.offset(0);
        make.left.equalTo(self.hearderView).with.offset(10);
        make.right.equalTo(self.hearderView).with.offset(-10);
        make.height.mas_equalTo(100);
        
    }];
    
    
    if ([ZDQSingleton shareSingleton].isLongin == YES) {
        self.tableView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.3 alpha:1];
        self.describeLabel.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.3 alpha:1];
        self.describeLabel.textColor = [UIColor whiteColor];
    } else {
        
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.describeLabel.textColor = [UIColor blackColor];
        self.describeLabel.backgroundColor = [UIColor whiteColor];
    }
    
    
//    photoView.backgroundColor = [UIColor redColor];
    self.banner = [SLBannerView bannerWithFrame:CGRectMake(0, 150 * KHEIGHT / 667, KWIDTH, 200 * KHEIGHT / 667) actionBlock:^(NSInteger index) {
        
        ZDQNewsWebTableViewController *newsWeb = [[ZDQNewsWebTableViewController alloc] init];
        [self.navigationController pushViewController:newsWeb animated:YES];
        ZDQNewsTopic *topic = self.imageArr[index];
        newsWeb.StrURL = topic.shareUrl;
        
        
    }];
    
    [self.hearderView addSubview:self.banner];
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText = @"正在加载中...";

    [self create];
    
}




- (void)create {
    
    [SLAFNetWorkTool cacheGetWithURL:self.strURL cookie:nil success:^(id responseObject) {
        
        NSDictionary * dic = responseObject;
        self.Arr = [ZDQNewsTopic baseModelByArr:dic[@"body"][@"subjects"]];
        ZDQNewsTopic *topic = self.Arr[0];
        ZDQNewsTopicMore *topicmore = [[ZDQNewsTopicMore alloc] init];
        [topicmore setValuesForKeysWithDictionary:topic.content];
        [self.photoImageView sd_setImageWithURL:[NSURL URLWithString:topicmore.customBanner] placeholderImage:[UIImage imageNamed:@"pac.png"]];
        ZDQNewsTopic * top = self.Arr[1];
        ZDQNewsTopicMore *more = [[ZDQNewsTopicMore alloc] init];
        [more setValuesForKeysWithDictionary:top.content];
        self.describeLabel.text = more.intro;
        
        self.array = [NSMutableArray array];
        for (NSInteger i = 3; i < self.Arr.count; i++) {
            
            [self.array addObject:self.Arr[i]];
        }
        
        ZDQNewsTopic * NEWtop = self.Arr[2];
        self.imageArr = [ZDQNewsTopic baseModelByArr:NEWtop.podItems];
        self.phototArray = [NSMutableArray array];
        self.titleArray = [NSMutableArray array];
        for (ZDQNewsTopic *top in self.imageArr) {
            
            [self.phototArray addObject:top.thumbnail];
            [self.titleArray addObject:top.title];
            
        }
        
        self.bannerImageURLArray = [self.phototArray copy];
        self.bannerTitleStrArray = [self.titleArray copy];
        
        self.banner.imageURLStrArray = self.bannerImageURLArray;
        self.banner.titleStrArray = self.bannerTitleStrArray;
        NSLog(@"%@", self.banner.imageURLStrArray);
        NSLog(@"%@", self.banner.titleStrArray);
        
        self.hud.hidden = YES;
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
    }];

    
}



#pragma mark - tableView的dataSource和delegate

//设置分区高度
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.array.count;

}


//设置分区名
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    ZDQNewsTopic *topic = self.array[section];
    return topic.title;
    
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    ZDQNewsTopic *topic = self.array[section];
    NSArray *Arr = [ZDQNewsTopic baseModelByArr:topic.podItems];
    
    if ([topic.title isEqualToString:@"视频报道"]) {
        
        return 1;
        
    } else {
        
    return Arr.count;
        
    }
}


//返回行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZDQNewsTopic *topic = self.array[indexPath.section];
    
    if ([topic.title isEqualToString:@"视频报道"]) {
        
        return 300 * KHEIGHT / 667;
        
    } else {
        
        return 100 * KHEIGHT / 667;
        
    }
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZDQNewsTopic *topic = self.array[indexPath.section];
    
    if ([topic.title isEqualToString:@"视频报道"])  {
        
        static NSString *reuse = @"Topicreuse";
        ZDQTopicNewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[ZDQTopicNewsTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
            
        }
        cell.backgroundColor = [UIColor clearColor];
        NSArray *array = [ZDQNewsTopic baseModelByArr:topic.podItems];
        cell.array = array;
        cell.delegate = self;
        return cell;
        
    } else {
    
        NSArray *Arr = [ZDQNewsTopic baseModelByArr:topic.podItems];
        ZDQNewsTopic * news = Arr[indexPath.row];
        
        if (news.thumbnail.length > 0) {
            
            static NSString *reuse = @"reuse";
            ZDQNewsTopicsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
            if (!cell) {
                cell = [[ZDQNewsTopicsTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
                
            }
            cell.backgroundColor = [UIColor clearColor];
            cell.titleLabel.text = news.title;
            [cell.photoImageView sd_setImageWithURL:[NSURL URLWithString:news.thumbnail] placeholderImage:[UIImage imageNamed:@"pac.png"]];
            if ([news.updateTime isEqualToString:@""]) {
                cell.timeLabel.text = @"";
            } else {
                
                cell.timeLabel.text = [news.updateTime substringToIndex:10];
                
            }
            cell.websiteLabel.text = news.source;
//
            if ([ZDQSingleton shareSingleton].isLongin == YES) {
                cell.titleLabel.textColor = [UIColor whiteColor];
                cell.photoImageView.alpha = 0.4;
                cell.timeLabel.textColor = [UIColor whiteColor];
                cell.websiteLabel.textColor = [UIColor whiteColor];
            } else {
                
                cell.titleLabel.textColor = [UIColor blackColor];
                cell.photoImageView.alpha = 1;
                cell.timeLabel.textColor = [UIColor blackColor];
                cell.websiteLabel.textColor = [UIColor blackColor];
                
            }
            return cell;
            
            
        } else {

            static NSString *reuse = @"Newsreuse";
            ZDQMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
            if (!cell) {
                cell = [[ZDQMoreTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
                
            }
            
            NSArray *array = [ZDQNewsTopic baseModelByArr:topic.podItems];
            ZDQNewsTopic *newstopic = array[indexPath.row];
            cell.titleLabel.text = newstopic.title;
            cell.timeLabel.text = [newstopic.updateTime substringWithRange:NSMakeRange(11, 5)];
            cell.websideLabel.text = newstopic.source;
            cell.backgroundColor = [UIColor clearColor];
            if ([ZDQSingleton shareSingleton].isLongin == YES) {
                cell.titleLabel.textColor = [UIColor whiteColor];
                cell.timeLabel.textColor = [UIColor whiteColor];
                cell.websideLabel.textColor = [UIColor whiteColor];
            } else {
                
                cell.titleLabel.textColor = [UIColor blackColor];
                cell.timeLabel.textColor = [UIColor blackColor];
                cell.websideLabel.textColor = [UIColor blackColor];
                
            }
            
            return cell;

    
        }
    }
}



#pragma mark tableView的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZDQNewsWebTableViewController *newsVC = [[ZDQNewsWebTableViewController alloc] init];
    [self.navigationController pushViewController:newsVC animated:YES];
    ZDQNewsTopic *topic = self.array[indexPath.section];
    NSArray *Arr = [ZDQNewsTopic baseModelByArr:topic.podItems];
    ZDQNewsTopic * news = Arr[indexPath.row];
    newsVC.StrURL = news.shareUrl;
    
}




#pragma mask 协议方法
- (void)didselect:(NSIndexPath *)indexPath {
    
    ZDQNewsWebTableViewController *newsVC = [[ZDQNewsWebTableViewController alloc] init];
    [self.navigationController pushViewController:newsVC animated:YES];
    
    NSArray *Arr = [NSArray array];
    for (ZDQNewsTopic *topic in self.array) {
        if ([topic.title isEqualToString:@"视频报道"]) {
            Arr = [ZDQNewsTopic baseModelByArr:topic.podItems];
        }
    }
    ZDQNewsTopic * news = Arr[indexPath.row];
    newsVC.StrURL = news.shareUrl;

    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
